package com.ischoolbar.programmer.servlet;

/**
 * @Author lin
 **/

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.ischoolbar.programmer.dao.StudentDao;
import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.model.Student;
import com.ischoolbar.programmer.util.SnGenerateUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentServlet extends HttpServlet {
    public StudentServlet() {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if ("toStudentListView".equals(method)) {
            this.studentList(request, response);
        } else if ("AddStudent".equals(method)) {
            this.addStudent(request, response);
        } else if ("StudentList".equals(method)) {
            this.getStudentList(request, response);
        } else if ("EditStudent".equals(method)) {
            this.editStudent(request, response);
        } else if ("DeleteStudent".equals(method)) {
            this.deleteStudent(request, response);
        }

    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        String idStr = "";
        String[] var5 = ids;
        int var6 = ids.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String id = var5[var7];
            idStr = idStr + id + ",";
        }

        idStr = idStr.substring(0, idStr.length() - 1);
        StudentDao studentDao = new StudentDao();
        if (studentDao.deleteStudent(idStr)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var12) {
                var12.printStackTrace();
            } finally {
                studentDao.closeCon();
            }
        }

    }

    private void editStudent(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        int id = Integer.parseInt(request.getParameter("id"));
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Student student = new Student();
        student.setClazzId(clazzId);
        student.setMobile(mobile);
        student.setName(name);
        student.setId(id);
        student.setQq(qq);
        student.setSex(sex);
        StudentDao studentDao = new StudentDao();
        if (studentDao.editStudent(student)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var15) {
                var15.printStackTrace();
            } finally {
                studentDao.closeCon();
            }
        }

    }

    private void getStudentList(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("studentName");
        Integer currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("rows") == null ? 999 : Integer.parseInt(request.getParameter("rows"));
        Integer clazz = request.getParameter("clazzid") == null ? 0 : Integer.parseInt(request.getParameter("clazzid"));
        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        Student student = new Student();
        student.setName(name);
        student.setClazzId(clazz);
        if (userType == 2) {
            Student currentUser = (Student)request.getSession().getAttribute("user");
            student.setId(currentUser.getId());
        }

        StudentDao studentDao = new StudentDao();
        List<Student> clazzList = studentDao.getStudentList(student, new Page(currentPage, pageSize));
        int total = studentDao.getStudentListTotal(student);
        studentDao.closeCon();
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> ret = new HashMap();
        ret.put("total", total);
        ret.put("rows", clazzList);

        try {
            String from = request.getParameter("from");
            if ("combox".equals(from)) {
                response.getWriter().write(JSONArray.fromObject(clazzList).toString());
            } else {
                response.getWriter().write(JSONObject.fromObject(ret).toString());
            }
        } catch (IOException var14) {
            var14.printStackTrace();
        }

    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Student student = new Student();
        student.setClazzId(clazzId);
        student.setMobile(mobile);
        student.setName(name);
        student.setPassword(password);
        student.setQq(qq);
        student.setSex(sex);
        student.setSn(SnGenerateUtil.generateStudentSn(clazzId));
        StudentDao studentDao = new StudentDao();
        if (studentDao.addStudent(student)) {
            try {
                response.getWriter().write("success");
            } catch (IOException var15) {
                var15.printStackTrace();
            } finally {
                studentDao.closeCon();
            }
        }

    }

    private void studentList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            request.getRequestDispatcher("view/studentList.jsp").forward(request, response);
        } catch (ServletException var4) {
            var4.printStackTrace();
        }

    }
}
